package com.example.restatr.Atcivity.PetHospital;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.restatr.Atcivity.Main.SearchAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.PetHospital.Bean.PawsClawsPetVetBean;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class PetHospitalAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ImageView searchIcon;
    private GridView card;
    private LinearLayout loading;
    private RecyclerView rv;
    private TextView bottomText;
    private List<CardBean> cardBeans = new ArrayList<>();
    private List<PawsClawsPetVetBean.RowsBean> rowsBeans = new ArrayList<>();
    private int pageNum = 1;
    private int total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_pet_hospital_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        searchIcon = findViewById( R.id.searchIcon );
        card = findViewById( R.id.card );
        loading = findViewById( R.id.loading );
        rv = findViewById( R.id.rv );
        bottomText = findViewById( R.id.bottom_Text );
        Tools.SetBarTitle( toolbar, barTitle, "宠物医院", search, this, true );
        searchIcon.setVisibility( View.VISIBLE );
        searchIcon.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( PetHospitalAt.this, SearchAt.class );
                intent.putExtra( "intent", "宠物医院" );
                startActivity( intent );
            }
        } );
        Menu menu = toolbar.getMenu();
        menu.getItem( 0 ).setTitle( "我的问诊" );
        menu.removeItem( R.id.item2 );
        toolbar.setOnMenuItemClickListener( new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity( new Intent( PetHospitalAt.this, MyConsultationAt.class ) );
                return true;
            }
        } );
        card.setNumColumns( 5 );
        Tools.SetCard( "/prod-api/api/pet-hospital/pet-type/list", MainActivity.sp.getString( "token", "" ), "url",
                "data", "id", "imgUrl", "name", this, cardBeans, card );
        GetDoctorList();
        card.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent( PetHospitalAt.this, PetDoctorListAt.class );
                intent.putExtra( "id", cardBeans.get( position ).getId() );
                intent.putExtra( "name", cardBeans.get( position ).getTitle() );
                startActivity( intent );
            }
        } );
        rv.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged( recyclerView, newState );
                if (!recyclerView.canScrollVertically( 1 ) && newState == 0) {
                    if (rowsBeans.size() < total) {
                        pageNum++;
                        GetDoctorList();
                    } else {
                        bottomText.setText( "——到底了——" );
                    }
                }
            }
        } );
    }


    private void GetDoctorList() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/pet-hospital/pet-doctor/list?pageSize=10&pageNum=" + pageNum, MainActivity.sp.getString( "token", "" ) );
            PawsClawsPetVetBean rowsBean = new Gson().fromJson( token, PawsClawsPetVetBean.class );
            total = rowsBean.getTotal();
            rowsBeans.addAll( rowsBean.getRows() );
            runOnUiThread( () -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility( View.GONE );
                    rv.setAdapter( new adapter() );
                }
            } );
        } ).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.lawyer_item, parent, false );
            return new viewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            Glide.with( PetHospitalAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getAvatar() )
                    .transform( new CircleCrop() )
                    .into( holder.head );
            holder.name.setText( rowsBeans.get( position ).getName() );
            holder.workStartAt.setText( "职业证号" + rowsBeans.get( position ).getPracticeNo() );
            holder.serviceTimes.setText( "从业时间 " + rowsBeans.get( position ).getWorkingYears() + "年" );
            holder.separate.setVisibility( View.GONE );
            holder.favorableRate.setVisibility( View.GONE );
            holder.legalExpertiseName.setText( rowsBeans.get( position ).getJobName() );
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent( PetHospitalAt.this, PetDoctorDetailsAt.class );
                    intent.putExtra( "data", new Gson().toJson( rowsBeans.get( position ) ) );
                    startActivity( intent );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView head;
            private TextView name;
            private TextView workStartAt;
            private TextView serviceTimes;
            private TextView favorableRate;
            private TextView legalExpertiseName;
            private CardView cardBtn;
            private View separate;


            public viewHolder(@NonNull View itemView) {
                super( itemView );
                head = itemView.findViewById( R.id.head );
                name = itemView.findViewById( R.id.name );
                workStartAt = itemView.findViewById( R.id.workStartAt );
                serviceTimes = itemView.findViewById( R.id.serviceTimes );
                favorableRate = itemView.findViewById( R.id.favorableRate );
                legalExpertiseName = itemView.findViewById( R.id.legalExpertiseName );
                cardBtn = itemView.findViewById( R.id.cardBtn );
                separate = itemView.findViewById( R.id.separate );
            }
        }
    }


}